// This file is part of OpenMeca, an easy software to do mechanical simulation.
//
// Author(s)    :  - Damien ANDRE  <openmeca@gmail.com>
//
// Copyright (C) 2012 Damien ANDRE
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <QApplication>
#include <iostream>

#include "OpenMeca/Gui/WidgetMatrix.hpp"
#include "OpenMeca/Util/Dimension.hpp"
#include "OpenMeca/Util/Unit.hpp"

namespace OpenMeca
{
  namespace Gui
  {
    WidgetMatrix::WidgetMatrix(QWidget* parent)
      :QWidget(parent),
       WidgetInterface<Geom::Matrix<_3D> >(*parent),
       dim_(0)
    {
      Ui::WidgetMatrix::setupUi(this);
      QObject::connect(&xy_->GetLineEdit(), SIGNAL(textChanged(const QString&)), 
		       this               , SLOT(XYEdited(const QString&))); 
      QObject::connect(&xz_->GetLineEdit(), SIGNAL(textChanged(const QString&)), 
		       this               , SLOT(XZEdited(const QString&))); 
      QObject::connect(&yz_->GetLineEdit(), SIGNAL(textChanged(const QString&)), 
		       this               , SLOT(YZEdited(const QString&))); 

    }

    WidgetMatrix::~WidgetMatrix()
    {
    }

    
    void 
    WidgetMatrix::SetDimension(const Util::Dimension& dim)
    {
      dim_ = &dim;

      xx_->SetDimension(dim);
      xy_->SetDimension(dim);
      xz_->SetDimension(dim);

      yx_->SetDimension(dim);
      yy_->SetDimension(dim);
      yz_->SetDimension(dim);

      zx_->SetDimension(dim);
      zy_->SetDimension(dim);
      zz_->SetDimension(dim);
    }


    void 
    WidgetMatrix::SetValue_CallBack()
    {
      Geom::Matrix<_3D>& m =  WidgetInterface<Geom::Matrix<_3D> >::GetValue();
      xx_->SetValue(m[0][0]);
      xy_->SetValue(m[0][1]);
      xz_->SetValue(m[0][2]);

      yx_->SetValue(m[1][0]);
      yy_->SetValue(m[1][1]);
      yz_->SetValue(m[1][2]);

      zx_->SetValue(m[2][0]);
      zy_->SetValue(m[2][1]);
      zz_->SetValue(m[2][2]);
    }

     void 
     WidgetMatrix::Init()
    {
      
    }
    

    void 
    WidgetMatrix::SetLabel(const QString& str)
    {
      groupBox_->setTitle(str);
    }
   
    bool
    WidgetMatrix::Check()
    {
      assert(yx_->GetLineEdit().text()==xy_->GetLineEdit().text());
      assert(zx_->GetLineEdit().text()==xz_->GetLineEdit().text());
      assert(zy_->GetLineEdit().text()==yz_->GetLineEdit().text());
      
      return (xx_->Check() & xy_->Check() & xz_->Check() &
	      yx_->Check() & yy_->Check() & yz_->Check() &
	      zx_->Check() & zy_->Check() & zz_->Check());
    }

    void
    WidgetMatrix::XYEdited(const QString& str)
    {
      yx_->GetLineEdit().setText(str);
    }

    void
    WidgetMatrix::XZEdited(const QString& str)
    {
      zx_->GetLineEdit().setText(str);
    }

    void
    WidgetMatrix::YZEdited(const QString& str)
    {
      zy_->GetLineEdit().setText(str);
    }

  }
}


